package com.beaven.imclient.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;

/**
 * Created by Beaven
 * <p>
 * Date on 2017/4/14 14:55
 */
public class ToastUtil {

  private static Toast toast;
  @SuppressLint("StaticFieldLeak") private static Context context;

  public static void initToast(@NonNull Context context) {
    ToastUtil.context = context.getApplicationContext();
  }

  public static void show(int resId) {
    showToast(context.getResources().getString(resId));
  }

  public static void show(CharSequence text) {
    showToast(text);
  }

  private static void showToast(CharSequence text) {
    if (context == null) {
      throw new IllegalStateException("ToastUtil not be init");
    }
    if (toast == null) {
      toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    } else {
      toast.setText(text);
    }
    toast.show();
  }
}
